<?php

namespace app\api\controller;

use app\api\logic\DownloadLogic;
use app\basic\ApiController;
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("下载记录")
 */
class DownloadController extends ApiController
{
    protected $logic;

    public function __construct()
    {
        $this->logic = new DownloadLogic();
        parent::__construct();
    }

    /**
     * @Apidoc\Title("获取下载记录列表")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("page", type="int", default="1", desc="页码")
     * @Apidoc\Param("limit", type="int", default="10", desc="每页数量")
     */
    public function getList()
    {
        $params = $this->request->only(['page', 'limit']);
        $list = $this->logic->getDownloadList($this->auth->getUserId(), $params);
        return $this->success($list);
    }

    /**
     * @Apidoc\Title("获取下载统计")
     * @Apidoc\Method("POST")
     */
    public function getStats()
    {
        $stats = $this->logic->getDownloadStats($this->auth->getUserId());
        return $this->success($stats);
    }

      /**
     * @Apidoc\Title("添加下载记录")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("article_id", type="int", require=true, desc="文档ID")
     * @Apidoc\Returned("data", type="object", desc="返回数据", children={
     *     @Apidoc\Returned("id", type="int", desc="下载记录ID"),
     *     @Apidoc\Returned("title", type="string", desc="文档标题"),
     *     @Apidoc\Returned("create_time", type="string", desc="创建时间")
     * })
     */
    public function add()
    {
        if (!$this->auth->isLogin()) {
            return $this->fail('请先登录');
        }
        $params = $this->request->post();
        $result = $this->logic->addDownload($this->auth->getUserId(), $params);
        if ($result === false) {
            return $this->fail($this->logic->getError());
        }
        return $this->success($result);
    }
} 